/* 
 * GrayRabbit Cms基于J2EE架构内容管理系统
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department：运营部
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.dao.dictionary.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.grayrabbit.cms.dao.dictionary.DictionaryTypeDao;
import org.grayrabbit.cms.entity.DictionaryType;
import org.grayrabbit.cms.vo.dictionary.DictionaryTypeBean;
import org.springframework.stereotype.Repository;

import org.grayrabbit.dao.impl.GenericHibernateDao;
import org.grayrabbit.dao.util.Page;
import org.grayrabbit.dao.util.QueryCondition;

/**
 * <b>DictionaryType</b> dao implementation 





 * @version 1.0，创建时间：2011-05-16 14:46:53 
 */
@Repository
public class DictionaryTypeDaoImpl extends GenericHibernateDao<DictionaryType, String>
		implements DictionaryTypeDao {

	/**
	 * HQL主体语句
	 */
	public static final String MAIN_HQL = "From DictionaryType as obj";
	/**
	 * HQL中count统计hql片段
	 */
	public static final String COUNT_HQL = "Select Count(obj) ";

	/**
	 * 根据bean组装查询条件
	 * @see DictionaryTypeDao#assembly(DictionaryTypeBean)
	 */
	public QueryCondition assembly(DictionaryTypeBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * 根据bean组装统计查询条件
	 * @see DictionaryTypeDao#assemblyCount(DictionaryTypeBean)
	 */
	public QueryCondition assemblyCount(DictionaryTypeBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * @see DictionaryTypeDao#count(DictionaryTypeBean)
	 */
	public int count(DictionaryTypeBean bean) {
		return ((Long)iterator(assemblyCount(bean)).next()).intValue();
	}

	/**
	 * 根据bean组装查询条件(名称参数)
	 * @see DictionaryTypeDao#assemblyNameQuery(DictionaryTypeBean)
	 */
	public QueryCondition assermblyNameQuery(DictionaryTypeBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		transNameQuery(bean, qc);
		return qc;
	}

	/**
	 * 根据bean组装统计查询条件(名称参数)
	 * @see DictionaryTypeDao#assemblyNameQueryCount(DictionaryTypeBean)
	 */
	public QueryCondition assemblyNameQueryCount(DictionaryTypeBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * 填充分页组件
	 * @see DictionaryTypeDao#findByPage(Page, DictionaryTypeBean)
	 */
	public List<DictionaryType> findByPage(Page<DictionaryType> page,
			DictionaryTypeBean bean) {
		QueryCondition qc = assembly(bean);
		qc.setMaxResults(page.getPageSize());
		qc.setOffset(page.getOffset());
		return findByQc(qc);
	}
	// 业务查询主要逻辑代码
	private void transNameQuery(DictionaryTypeBean bean,QueryCondition qc){
		if(bean!=null){
			if(bean.getDicTypeCode() != null){
				qc.add("And obj.dicTypeCode like ? ", "%"+bean.getDicTypeCode()+"%");
			}
            if(bean.getDicTypeName() != null){
				qc.add("And obj.dicTypeName like ? ", bean.getDicTypeName()+"%");
			}
            if(bean.getIsModify() != null){
            	qc.add("And obj.isModify = ? ", bean.getIsModify());
			}
            if(bean.getIsEnable() != null){
            	qc.add("And obj.isEnable = ? ", bean.getIsEnable());
			}
            if(bean.getMemo() != null){
				qc.add("And obj.memo like ? ", bean.getMemo()+"%");
			}

		}
	}

	public DictionaryType findByKey(String key) {
		if(StringUtils.isBlank(key)) return null;
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		qc.add(" And obj.dicTypeCode = ?", key); 
		List<DictionaryType> list = findByQc(qc);
		if(list == null || list .isEmpty()){
			return null;
		}else {
			return list.get(0);
		}
	}


}